/*
 * Copyright (C) 2011  Xu Haojie
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.poemcode.gzlib;

import java.util.ArrayList;
import java.util.List;

import net.poemcode.gzlib.detail.DetailRequest;
import net.poemcode.gzlib.detail.DetailResult;
import net.poemcode.gzlib.detail.DetailThread;
import android.app.Dialog;
import android.app.ExpandableListActivity;
import android.app.ProgressDialog;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class DetailActivity extends ExpandableListActivity {
	
	private BaseExpandableListAdapter mAdapter;
	
	private String mBookTitle;
	
	private static final int DIALOG_WAIT_ID = 0;
	
	private Handler mHandler = new Handler() {

		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case DetailThread.MSG_DETAIL_ID:
				DetailActivity.this.dismissDialog(DIALOG_WAIT_ID);
				
				List<DetailResult> list;
				if (msg.obj == null) {
					list = new ArrayList<DetailResult>();
				} else {
					list = (List<DetailResult>) msg.obj;
				}
				mAdapter = new DetailExpandableListAdapter(new ArrayList<DetailResult>(list));
				setListAdapter(mAdapter);
				break;
			default:
				break;
			}
		}
		
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.detail);
		
		String bookrecno = this.getIntent().getStringExtra("bookrecno");
		mBookTitle = this.getIntent().getStringExtra("booktitle");
		DetailRequest request = new DetailRequest(bookrecno);
		DetailThread thread = new DetailThread(this, mHandler, request);
		new Thread(thread).start();
		
		mAdapter = new DetailExpandableListAdapter(new ArrayList<DetailResult>());
        setListAdapter(mAdapter);
        
        showDialog(DIALOG_WAIT_ID);
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_WAIT_ID:
			ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
			return dialog;
		default:
			return null;	
		}
	}

	public class DetailExpandableListAdapter extends BaseExpandableListAdapter{
		
		private ArrayList<DetailResult> mList;
		
		private LayoutInflater mInflater;
		
		private Resources mRes;

		public DetailExpandableListAdapter(ArrayList<DetailResult> list) {
			this.mList = list;
			this.mInflater = DetailActivity.this.getLayoutInflater();
			this.mRes = DetailActivity.this.getResources();
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			DetailResult detail = mList.get(groupPosition);
			switch(childPosition) {
			case 0:
				return mRes.getString(R.string.detail_barcode) + detail.getBarcode();
			case 1:
				return mRes.getString(R.string.detail_callno) + detail.getCallNo();
			case 2:
				return mRes.getString(R.string.detail_cirtype) + Cache.getInstance(DetailActivity.this).getCirName(detail.getCirType());
			case 3:
				return mRes.getString(R.string.detail_curllib) + Cache.getInstance(DetailActivity.this).getLibName(detail.getCurlLib());
			case 4:
				return mRes.getString(R.string.detail_curlocal) + Cache.getInstance(DetailActivity.this).getLocalName(detail.getCurLocal());
			case 5:
				return mRes.getString(R.string.detail_state) + Cache.getInstance(DetailActivity.this).getStateName(detail.getState());
			default:
				return null;
			}
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			View childView = mInflater.inflate(R.layout.detail_child, null);
            TextView titleView = (TextView) childView.findViewById(R.id.detailValue);
            titleView.setText(getChild(groupPosition, childPosition).toString());
            return childView;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return 6;
		}

		@Override
		public Object getGroup(int groupPosition) {
			return mList.get(groupPosition).getBarcode();
		}

		@Override
		public int getGroupCount() {
			return mList.size();
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			View groupView = mInflater.inflate(R.layout.detail_group, null);
            TextView titleView = (TextView) groupView.findViewById(R.id.barcode);
            String vol = mList.get(groupPosition).getVolInfo();
            vol = (vol == null) ? "" : " (" + vol + ")"; 
            titleView.setText(mBookTitle + vol);
            return groupView;
		}

		@Override
		public boolean hasStableIds() {
			return true;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return false;
		}
		
	}
}
